package cn.waka.sys.admin.service;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CompleteMultipartUploadResult;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.UploadFileRequest;
import com.aliyun.oss.model.UploadFileResult;

@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class FileUploadService {

	private String endpoint;
	private String accessKeyId;//: LTAIxXFdbktLGKPG
    private String accessKeySecret;//: mXKTipBbsQwyCqe4MJdbC2sQcXjMWT
    private String bucketName;//: vr-dev  
	public void setEndpoint(String endpoint) {
		this.endpoint = endpoint;
	}
	public void setAccessKeyId(String accessKeyId) {
		this.accessKeyId = accessKeyId;
	}
	public void setAccessKeySecret(String accessKeySecret) {
		this.accessKeySecret = accessKeySecret;
	}
	public void setBucketName(String bucketName) {
		this.bucketName = bucketName;
	}
	
	
	public String upload(String name,String uploadFile) {
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		ObjectMetadata metadata = new ObjectMetadata();
		metadata.setContentType(getContentType(name));  
		UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, name);
        // 待上传的本地文件
        uploadFileRequest.setUploadFile(uploadFile);
        // 设置并发下载数，默认1
        uploadFileRequest.setTaskNum(5);
        // 设置分片大小，默认100KB
        uploadFileRequest.setPartSize(1024 * 1024 * 10);
        
        // 开启断点续传，默认关闭
        uploadFileRequest.setEnableCheckpoint(true);
        uploadFileRequest.setObjectMetadata(metadata);
		try {
			 UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
			 
			 CompleteMultipartUploadResult result = 
		                uploadResult.getMultipartUploadResult();
			 return result.getLocation();
		} catch (Throwable e) {
			e.printStackTrace();
		}
        
        
		return null;
	}
    
	 public static  String getContentType(String fileName){  
	        //文件的后缀名  
	        String fileExtension = fileName.substring(fileName.lastIndexOf("."));  
	        if(".bmp".equalsIgnoreCase(fileExtension)) {  
	            return "image/bmp";  
	        }  
	        if(".gif".equalsIgnoreCase(fileExtension)) {  
	            return "image/gif";  
	        }  
	        if(".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension)  || ".png".equalsIgnoreCase(fileExtension) ) {  
	            return "image/jpeg";  
	        }  
	        if(".html".equalsIgnoreCase(fileExtension)) {  
	            return "text/html";  
	        }  
	        if(".txt".equalsIgnoreCase(fileExtension)) {  
	            return "text/plain";  
	        }  
	        if(".vsd".equalsIgnoreCase(fileExtension)) {  
	            return "application/vnd.visio";  
	        }  
	        if(".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {  
	            return "application/vnd.ms-powerpoint";  
	        }  
	        if(".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {  
	            return "application/msword";  
	        }  
	        if(".xml".equalsIgnoreCase(fileExtension)) {  
	            return "text/xml";  
	        }  
	        //默认返回类型  
	        return "image/jpeg";  
	    }  
    
	
}
